﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibKetoan;
using doiso;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmHopdong.
	/// </summary>
	public class frmHopdong : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button butMoi;
		private System.Windows.Forms.Button butSua;
		private System.Windows.Forms.Button butLuu;
		private System.Windows.Forms.Button butBoqua;
		private System.Windows.Forms.Button butHuy;
		private System.Windows.Forms.Button butIn;
		private System.Windows.Forms.Button butKetthuc;
		private string sql,format_sotien,table="a_theodoihd",user;
		private int i_userid,itable,i_loai;
		private long l_id,l_idold,l_denghi;
		private bool bNew,bAdmin;
		private AccessData a;
		private DataTable dtct=new DataTable();
		private DataTable dtdt=new DataTable();
		private DataTable dt=new DataTable();
		private DataRow r;
		private System.Windows.Forms.TextBox stt;
		private System.Windows.Forms.TextBox find;
		private System.Windows.Forms.CheckBox chkIn;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Label label37;
		private System.Windows.Forms.Label label13;
		private string s_mmyy,s_ngay,ngayts;
		private System.Windows.Forms.TextBox stemp;
		private long lmadt;
		private LibList.List listdt;
		private System.Windows.Forms.TextBox tendt;
		private System.Windows.Forms.TextBox madt;
		private System.Windows.Forms.Label label27;
		private MaskedTextBox.MaskedTextBox diachi;
		private System.Windows.Forms.Label label46;
		private System.Windows.Forms.TextBox noidung;
		private System.Windows.Forms.TextBox sotien;
		private System.Windows.Forms.DateTimePicker ngayqd;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox soqd;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox sohd;
		private System.Windows.Forms.DateTimePicker ngayhd;
		private System.Windows.Forms.Label label7;
		private MaskedTextBox.MaskedTextBox laisuat;
		private System.Windows.Forms.DataGrid dataGrid2;
		private System.Windows.Forms.Label label3;

		public frmHopdong(AccessData acc,string mmyy,string _ngay,int userid,bool admin)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;i_userid=userid;s_mmyy=mmyy;	ngayts=s_ngay=_ngay;bAdmin=admin;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmHopdong));
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.butMoi = new System.Windows.Forms.Button();
			this.butSua = new System.Windows.Forms.Button();
			this.butLuu = new System.Windows.Forms.Button();
			this.butBoqua = new System.Windows.Forms.Button();
			this.butHuy = new System.Windows.Forms.Button();
			this.butIn = new System.Windows.Forms.Button();
			this.butKetthuc = new System.Windows.Forms.Button();
			this.stt = new System.Windows.Forms.TextBox();
			this.find = new System.Windows.Forms.TextBox();
			this.chkIn = new System.Windows.Forms.CheckBox();
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.laisuat = new MaskedTextBox.MaskedTextBox();
			this.label37 = new System.Windows.Forms.Label();
			this.noidung = new System.Windows.Forms.TextBox();
			this.label13 = new System.Windows.Forms.Label();
			this.stemp = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.listdt = new LibList.List();
			this.tendt = new System.Windows.Forms.TextBox();
			this.madt = new System.Windows.Forms.TextBox();
			this.label27 = new System.Windows.Forms.Label();
			this.diachi = new MaskedTextBox.MaskedTextBox();
			this.label46 = new System.Windows.Forms.Label();
			this.sotien = new System.Windows.Forms.TextBox();
			this.ngayqd = new System.Windows.Forms.DateTimePicker();
			this.label5 = new System.Windows.Forms.Label();
			this.soqd = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.sohd = new System.Windows.Forms.TextBox();
			this.ngayhd = new System.Windows.Forms.DateTimePicker();
			this.label7 = new System.Windows.Forms.Label();
			this.dataGrid2 = new System.Windows.Forms.DataGrid();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
			this.SuspendLayout();
			// 
			// dataGrid1
			// 
			this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
			this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
			this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
			this.dataGrid1.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid1.DataMember = "";
			this.dataGrid1.FlatMode = true;
			this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
			this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
			this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
			this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
			this.dataGrid1.Location = new System.Drawing.Point(8, 8);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
			this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid1.ReadOnly = true;
			this.dataGrid1.RowHeaderWidth = 10;
			this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
			this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid1.Size = new System.Drawing.Size(776, 256);
			this.dataGrid1.TabIndex = 90;
			this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
			// 
			// butMoi
			// 
			this.butMoi.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butMoi.Image = ((System.Drawing.Bitmap)(resources.GetObject("butMoi.Image")));
			this.butMoi.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butMoi.Location = new System.Drawing.Point(172, 491);
			this.butMoi.Name = "butMoi";
			this.butMoi.Size = new System.Drawing.Size(60, 25);
			this.butMoi.TabIndex = 12;
			this.butMoi.Text = "      &Mới";
			this.butMoi.Click += new System.EventHandler(this.butMoi_Click);
			// 
			// butSua
			// 
			this.butSua.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butSua.Image = ((System.Drawing.Bitmap)(resources.GetObject("butSua.Image")));
			this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butSua.Location = new System.Drawing.Point(235, 491);
			this.butSua.Name = "butSua";
			this.butSua.Size = new System.Drawing.Size(60, 25);
			this.butSua.TabIndex = 13;
			this.butSua.Text = "     &Sửa";
			this.butSua.Click += new System.EventHandler(this.butSua_Click);
			// 
			// butLuu
			// 
			this.butLuu.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butLuu.Enabled = false;
			this.butLuu.Image = ((System.Drawing.Bitmap)(resources.GetObject("butLuu.Image")));
			this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butLuu.Location = new System.Drawing.Point(298, 491);
			this.butLuu.Name = "butLuu";
			this.butLuu.Size = new System.Drawing.Size(60, 25);
			this.butLuu.TabIndex = 10;
			this.butLuu.Text = "     &Lưu";
			this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
			// 
			// butBoqua
			// 
			this.butBoqua.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butBoqua.Enabled = false;
			this.butBoqua.Image = ((System.Drawing.Bitmap)(resources.GetObject("butBoqua.Image")));
			this.butBoqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butBoqua.Location = new System.Drawing.Point(361, 491);
			this.butBoqua.Name = "butBoqua";
			this.butBoqua.Size = new System.Drawing.Size(60, 25);
			this.butBoqua.TabIndex = 11;
			this.butBoqua.Text = "&Bỏ qua";
			this.butBoqua.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butBoqua.Click += new System.EventHandler(this.butBoqua_Click);
			// 
			// butHuy
			// 
			this.butHuy.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butHuy.Image = ((System.Drawing.Bitmap)(resources.GetObject("butHuy.Image")));
			this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butHuy.Location = new System.Drawing.Point(424, 491);
			this.butHuy.Name = "butHuy";
			this.butHuy.Size = new System.Drawing.Size(60, 25);
			this.butHuy.TabIndex = 14;
			this.butHuy.Text = "     &Hủy";
			this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
			// 
			// butIn
			// 
			this.butIn.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butIn.Image = ((System.Drawing.Bitmap)(resources.GetObject("butIn.Image")));
			this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butIn.Location = new System.Drawing.Point(487, 491);
			this.butIn.Name = "butIn";
			this.butIn.Size = new System.Drawing.Size(60, 25);
			this.butIn.TabIndex = 15;
			this.butIn.Text = "      &In";
			this.butIn.Click += new System.EventHandler(this.butIn_Click);
			// 
			// butKetthuc
			// 
			this.butKetthuc.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butKetthuc.Image = ((System.Drawing.Bitmap)(resources.GetObject("butKetthuc.Image")));
			this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butKetthuc.Location = new System.Drawing.Point(550, 491);
			this.butKetthuc.Name = "butKetthuc";
			this.butKetthuc.Size = new System.Drawing.Size(70, 25);
			this.butKetthuc.TabIndex = 16;
			this.butKetthuc.Text = "&Kết thúc";
			this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
			// 
			// stt
			// 
			this.stt.Enabled = false;
			this.stt.Location = new System.Drawing.Point(64, 225);
			this.stt.Name = "stt";
			this.stt.Size = new System.Drawing.Size(40, 20);
			this.stt.TabIndex = 91;
			this.stt.Text = "";
			// 
			// find
			// 
			this.find.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.find.BackColor = System.Drawing.SystemColors.HighlightText;
			this.find.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.find.ForeColor = System.Drawing.Color.Red;
			this.find.Location = new System.Drawing.Point(8, 0);
			this.find.Name = "find";
			this.find.Size = new System.Drawing.Size(776, 21);
			this.find.TabIndex = 89;
			this.find.Text = "Tìm kiếm";
			this.find.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.find.TextChanged += new System.EventHandler(this.find_TextChanged);
			this.find.Enter += new System.EventHandler(this.find_Enter);
			// 
			// chkIn
			// 
			this.chkIn.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.chkIn.Location = new System.Drawing.Point(680, 488);
			this.chkIn.Name = "chkIn";
			this.chkIn.Size = new System.Drawing.Size(104, 16);
			this.chkIn.TabIndex = 98;
			this.chkIn.Text = "Xem trước khi in";
			// 
			// laisuat
			// 
			this.laisuat.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.laisuat.BackColor = System.Drawing.SystemColors.HighlightText;
			this.laisuat.Enabled = false;
			this.laisuat.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.laisuat.Location = new System.Drawing.Point(528, 461);
			this.laisuat.Masked = MaskedTextBox.MaskedTextBox.Mask.Decimal;
			this.laisuat.Name = "laisuat";
			this.laisuat.Size = new System.Drawing.Size(53, 21);
			this.laisuat.TabIndex = 8;
			this.laisuat.Text = "";
			this.laisuat.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.laisuat.Validated += new System.EventHandler(this.laisuat_Validated);
			// 
			// label37
			// 
			this.label37.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.label37.Location = new System.Drawing.Point(480, 462);
			this.label37.Name = "label37";
			this.label37.Size = new System.Drawing.Size(120, 23);
			this.label37.TabIndex = 73;
			this.label37.Text = "Lãi suất :                    %";
			this.label37.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// noidung
			// 
			this.noidung.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.noidung.BackColor = System.Drawing.SystemColors.HighlightText;
			this.noidung.Enabled = false;
			this.noidung.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.noidung.Location = new System.Drawing.Point(83, 461);
			this.noidung.Name = "noidung";
			this.noidung.Size = new System.Drawing.Size(397, 21);
			this.noidung.TabIndex = 7;
			this.noidung.Text = "";
			this.noidung.KeyDown += new System.Windows.Forms.KeyEventHandler(this.soqd_KeyDown);
			// 
			// label13
			// 
			this.label13.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label13.Location = new System.Drawing.Point(0, 462);
			this.label13.Name = "label13";
			this.label13.Size = new System.Drawing.Size(80, 23);
			this.label13.TabIndex = 51;
			this.label13.Text = "Nội dung :";
			this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// stemp
			// 
			this.stemp.Enabled = false;
			this.stemp.Location = new System.Drawing.Point(576, 224);
			this.stemp.Name = "stemp";
			this.stemp.TabIndex = 103;
			this.stemp.Text = "";
			// 
			// label3
			// 
			this.label3.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.label3.Location = new System.Drawing.Point(600, 462);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(48, 23);
			this.label3.TabIndex = 109;
			this.label3.Text = "Số tiền :";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// listdt
			// 
			this.listdt.BackColor = System.Drawing.SystemColors.Info;
			this.listdt.ColumnCount = 0;
			this.listdt.Location = new System.Drawing.Point(464, 536);
			this.listdt.MatchBufferTimeOut = 1000;
			this.listdt.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
			this.listdt.Name = "listdt";
			this.listdt.Size = new System.Drawing.Size(75, 17);
			this.listdt.TabIndex = 117;
			this.listdt.TextIndex = -1;
			this.listdt.TextMember = null;
			this.listdt.ValueIndex = -1;
			this.listdt.Visible = false;
			this.listdt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listdt_KeyDown);
			// 
			// tendt
			// 
			this.tendt.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.tendt.BackColor = System.Drawing.SystemColors.HighlightText;
			this.tendt.Enabled = false;
			this.tendt.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.tendt.Location = new System.Drawing.Point(154, 415);
			this.tendt.Name = "tendt";
			this.tendt.Size = new System.Drawing.Size(630, 21);
			this.tendt.TabIndex = 5;
			this.tendt.Text = "";
			this.tendt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tendt_KeyDown);
			this.tendt.TextChanged += new System.EventHandler(this.tendt_TextChanged);
			// 
			// madt
			// 
			this.madt.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.madt.BackColor = System.Drawing.SystemColors.HighlightText;
			this.madt.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.madt.Enabled = false;
			this.madt.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.madt.Location = new System.Drawing.Point(83, 415);
			this.madt.Name = "madt";
			this.madt.Size = new System.Drawing.Size(70, 21);
			this.madt.TabIndex = 4;
			this.madt.Text = "";
			this.madt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.hoatdong_KeyDown);
			this.madt.Validated += new System.EventHandler(this.madt_Validated);
			// 
			// label27
			// 
			this.label27.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label27.Location = new System.Drawing.Point(0, 415);
			this.label27.Name = "label27";
			this.label27.Size = new System.Drawing.Size(80, 23);
			this.label27.TabIndex = 120;
			this.label27.Text = "Đối tượng :";
			this.label27.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// diachi
			// 
			this.diachi.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.diachi.BackColor = System.Drawing.SystemColors.HighlightText;
			this.diachi.Enabled = false;
			this.diachi.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.diachi.Location = new System.Drawing.Point(83, 438);
			this.diachi.Masked = MaskedTextBox.MaskedTextBox.Mask.None;
			this.diachi.Name = "diachi";
			this.diachi.Size = new System.Drawing.Size(701, 21);
			this.diachi.TabIndex = 6;
			this.diachi.Text = "";
			// 
			// label46
			// 
			this.label46.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label46.Location = new System.Drawing.Point(16, 438);
			this.label46.Name = "label46";
			this.label46.Size = new System.Drawing.Size(64, 23);
			this.label46.TabIndex = 123;
			this.label46.Text = "Địa chỉ :";
			this.label46.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// sotien
			// 
			this.sotien.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.sotien.BackColor = System.Drawing.SystemColors.HighlightText;
			this.sotien.Enabled = false;
			this.sotien.Location = new System.Drawing.Point(651, 462);
			this.sotien.Name = "sotien";
			this.sotien.Size = new System.Drawing.Size(133, 20);
			this.sotien.TabIndex = 9;
			this.sotien.Text = "";
			this.sotien.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.sotien.KeyDown += new System.Windows.Forms.KeyEventHandler(this.hoatdong_KeyDown);
			this.sotien.Validated += new System.EventHandler(this.sotien_Validated);
			// 
			// ngayqd
			// 
			this.ngayqd.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.ngayqd.CustomFormat = "dd/MM/yyyy";
			this.ngayqd.Enabled = false;
			this.ngayqd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.ngayqd.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
			this.ngayqd.Location = new System.Drawing.Point(264, 392);
			this.ngayqd.Name = "ngayqd";
			this.ngayqd.Size = new System.Drawing.Size(80, 21);
			this.ngayqd.TabIndex = 1;
			this.ngayqd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.soqd_KeyDown);
			// 
			// label5
			// 
			this.label5.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label5.Location = new System.Drawing.Point(0, 391);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(80, 23);
			this.label5.TabIndex = 115;
			this.label5.Text = "Số quyết định :";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// soqd
			// 
			this.soqd.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.soqd.BackColor = System.Drawing.SystemColors.HighlightText;
			this.soqd.Enabled = false;
			this.soqd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.soqd.Location = new System.Drawing.Point(83, 392);
			this.soqd.Name = "soqd";
			this.soqd.Size = new System.Drawing.Size(141, 21);
			this.soqd.TabIndex = 0;
			this.soqd.Text = "";
			this.soqd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.soqd_KeyDown);
			// 
			// label4
			// 
			this.label4.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label4.Location = new System.Drawing.Point(224, 391);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(40, 23);
			this.label4.TabIndex = 137;
			this.label4.Text = "Ngày :";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label6
			// 
			this.label6.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.label6.Location = new System.Drawing.Point(664, 391);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(40, 23);
			this.label6.TabIndex = 141;
			this.label6.Text = "Ngày :";
			this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// sohd
			// 
			this.sohd.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.sohd.BackColor = System.Drawing.SystemColors.HighlightText;
			this.sohd.Enabled = false;
			this.sohd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.sohd.Location = new System.Drawing.Point(408, 392);
			this.sohd.Name = "sohd";
			this.sohd.Size = new System.Drawing.Size(256, 21);
			this.sohd.TabIndex = 2;
			this.sohd.Text = "";
			this.sohd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.soqd_KeyDown);
			// 
			// ngayhd
			// 
			this.ngayhd.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.ngayhd.CustomFormat = "dd/MM/yyyy";
			this.ngayhd.Enabled = false;
			this.ngayhd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.ngayhd.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
			this.ngayhd.Location = new System.Drawing.Point(704, 392);
			this.ngayhd.Name = "ngayhd";
			this.ngayhd.Size = new System.Drawing.Size(80, 21);
			this.ngayhd.TabIndex = 3;
			this.ngayhd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.soqd_KeyDown);
			// 
			// label7
			// 
			this.label7.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label7.Location = new System.Drawing.Point(328, 391);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(80, 23);
			this.label7.TabIndex = 138;
			this.label7.Text = "Số hóa đơn :";
			this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// dataGrid2
			// 
			this.dataGrid2.AlternatingBackColor = System.Drawing.Color.Lavender;
			this.dataGrid2.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.dataGrid2.BackColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid2.BackgroundColor = System.Drawing.SystemColors.Control;
			this.dataGrid2.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.dataGrid2.CaptionBackColor = System.Drawing.SystemColors.Control;
			this.dataGrid2.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid2.CaptionForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid2.DataMember = "";
			this.dataGrid2.FlatMode = true;
			this.dataGrid2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid2.ForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid2.GridLineColor = System.Drawing.Color.Gainsboro;
			this.dataGrid2.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
			this.dataGrid2.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
			this.dataGrid2.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid2.LinkColor = System.Drawing.Color.Teal;
			this.dataGrid2.Location = new System.Drawing.Point(8, 264);
			this.dataGrid2.Name = "dataGrid2";
			this.dataGrid2.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
			this.dataGrid2.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid2.ReadOnly = true;
			this.dataGrid2.RowHeaderWidth = 10;
			this.dataGrid2.SelectionBackColor = System.Drawing.Color.CadetBlue;
			this.dataGrid2.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid2.Size = new System.Drawing.Size(776, 120);
			this.dataGrid2.TabIndex = 142;
			// 
			// frmHopdong
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(792, 573);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.dataGrid2,
																		  this.soqd,
																		  this.ngayqd,
																		  this.label6,
																		  this.sohd,
																		  this.ngayhd,
																		  this.label7,
																		  this.label4,
																		  this.sotien,
																		  this.tendt,
																		  this.diachi,
																		  this.madt,
																		  this.label27,
																		  this.label46,
																		  this.listdt,
																		  this.label5,
																		  this.label3,
																		  this.noidung,
																		  this.label13,
																		  this.laisuat,
																		  this.label37,
																		  this.chkIn,
																		  this.find,
																		  this.butKetthuc,
																		  this.butIn,
																		  this.butHuy,
																		  this.butBoqua,
																		  this.butLuu,
																		  this.butSua,
																		  this.butMoi,
																		  this.dataGrid1,
																		  this.stt,
																		  this.stemp});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "frmHopdong";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Hợp đồng";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.frmHopdong_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void frmHopdong_Load(object sender, System.EventArgs e)
		{			
			chkIn.Checked=a.bPreview;user=a.user;
			format_sotien=a.format_sotien(0);

			dtdt=a.get_data("select mavt,tendt,diachi,loaidt,id from a_dmdt where loaidt=1 and sudung=1 order by stt").Tables[0];
			listdt.DisplayMember="MAVT";
			listdt.ValueMember="TENDT";
			listdt.DataSource=dtdt;

			load_grid();
			AddGridTableStyle();
			ref_text(0);
			AddGridTableStyle1();
		}

		private void load_grid()
		{
			dataGrid1.DataSource=null;
			sql="select a.id,to_char(a.ngay,'dd/mm/yyyy') as ngay,a.soqd,to_char(a.ngayqd,'dd/mm/yyyy') as ngayqd,a.sohd,to_char(a.ngayhd,'dd/mm/yyyy') as ngayhd,";
			sql+="a.noidung,a.madt,b.mavt,b.tendt,b.diachi,a.loai,a.stien,a.stdatra,a.stien-a.stdatra as conlai,a.denghi,a.laisuat,a.mmyy,to_char(a.ngayqd,'yymmdd') yymmdd";
			sql+=" from "+table+" a inner join a_dmdt b on a.madt=b.id ";
			sql+=" where a.id<>0 ";
			if (bAdmin) sql+=" and a.userid="+i_userid;
			sql+=" order by yymmdd,soqd";
			dtct=a.get_data(sql).Tables[0];
			dataGrid1.DataSource=dtct;
		}

		private void AddGridTableStyle1()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = dtct.TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			ts.ReadOnly=false;
			ts.RowHeaderWidth=10;
						
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "stt";
			TextCol.HeaderText = "Đợt";
			TextCol.Width = 30;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "so";
			TextCol.HeaderText = "Số CT";
			TextCol.Width = 60;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ngay";
			TextCol.HeaderText = "Ngày";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "no";
			TextCol.HeaderText = "TK Nợ";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "co";
			TextCol.HeaderText = "TK Có";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sotien";
			TextCol.HeaderText = "Số tiền";
			TextCol.Alignment=HorizontalAlignment.Right;
			TextCol.Format="### ### ### ### ###";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ghichu";
			TextCol.HeaderText = "Ghi chú";
			TextCol.Width = dataGrid2.Width-(20+10+30+60+80+80+80+80);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);			
		}

		private void ref_text(long id)
		{
			try
			{
				int i=dataGrid1.CurrentCell.RowNumber;
				l_id=(id==0)?long.Parse(dataGrid1[i,0].ToString()):l_id;
				r=a.getrowbyid(dtct,"id="+l_id);
				if (r!=null)
				{
					soqd.Text=r["soqd"].ToString();
					ngayqd.Value=new DateTime(int.Parse(r["ngayqd"].ToString().Substring(6,4)),int.Parse(r["ngayqd"].ToString().Substring(3,2)),int.Parse(r["ngayqd"].ToString().Substring(0,2)),0,0,0,0);
					sohd.Text=r["sohd"].ToString();
					ngayhd.Value=new DateTime(int.Parse(r["ngayhd"].ToString().Substring(6,4)),int.Parse(r["ngayhd"].ToString().Substring(3,2)),int.Parse(r["ngayhd"].ToString().Substring(0,2)),0,0,0,0);
					s_ngay=r["ngay"].ToString();
					decimal st=decimal.Parse(r["stien"].ToString());
					sotien.Text=st.ToString(format_sotien);
					s_mmyy=r["mmyy"].ToString();
					i_loai=int.Parse(r["loai"].ToString());
					st=decimal.Parse(r["laisuat"].ToString());
					laisuat.Text=st.ToString("###,###.##");
					madt.Text=r["mavt"].ToString();
					tendt.Text=r["tendt"].ToString();
					diachi.Text=r["diachi"].ToString();
					noidung.Text=r["noidung"].ToString();
					l_denghi=long.Parse(r["denghi"].ToString());
					dataGrid2.CaptionText=tendt.Text+", Số hóa đơn :"+sohd.Text+", ngày :"+ngayhd.Text;
				}
			}
			catch{emp_detail();}
			load_gridct();
		}

		private void load_gridct()
		{
			sql="select id,stt,so,to_char(ngay,'dd/mm/yyyy') as ngay,no,co,sotien,ghichu from "+user+".a_theodoihdct where id="+l_id+" order by stt";
			dt=a.get_data(sql).Tables[0];
			dataGrid2.DataSource=dt;
			CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid2.DataSource,dataGrid2.DataMember]; 
			DataView dv = (DataView) cm.List; 
			dv.AllowNew = false; 
		}

		public Color MyGetColorRowCol(int row, int col)
		{
			if (this.dataGrid1[row,8].ToString()=="0") return Color.Blue;
			else return Color.Black;
		}

		public delegate Color delegateGetColorRowCol(int row, int col);
		public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
		{
			private delegateGetColorRowCol _getColorRowCol;
			private int _column;
			public DataGridColoredTextBoxColumn(delegateGetColorRowCol getcolorRowCol, int column)
			{
				_getColorRowCol = getcolorRowCol;
				_column = column;
			}
			protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
			{
				try
				{
					foreBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));
				}
				catch{}
				finally
				{
					base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
				}
			}
		}

		private void AddGridTableStyle()
		{
			DataGridColoredTextBoxColumn TextCol;
			delegateGetColorRowCol de = new delegateGetColorRowCol(MyGetColorRowCol);
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = dtct.TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			ts.ReadOnly=false;
			ts.RowHeaderWidth=10;
			
			TextCol=new DataGridColoredTextBoxColumn(de, 0);
			TextCol.MappingName = "id";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridColoredTextBoxColumn(de, 1);
			TextCol.MappingName = "soqd";
			TextCol.HeaderText = "Số quyết định";
			TextCol.Width = 100;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridColoredTextBoxColumn(de, 2);
			TextCol.MappingName = "ngayqd";
			TextCol.HeaderText = "Ngày";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridColoredTextBoxColumn(de, 3);
			TextCol.MappingName = "sohd";
			TextCol.HeaderText = "Số hóa đơn";
			TextCol.Width = 100;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridColoredTextBoxColumn(de, 4);
			TextCol.MappingName = "ngayhd";
			TextCol.HeaderText = "Ngày";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridColoredTextBoxColumn(de,5);
			TextCol.MappingName = "tendt";
			TextCol.HeaderText = "Đối tượng";
			TextCol.Width = dataGrid1.Width-(30+100+80+100+80+100+100+100);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridColoredTextBoxColumn(de, 6);
			TextCol.MappingName = "stien";
			TextCol.HeaderText = "Số tiền";
			TextCol.Width = 100;
			TextCol.Alignment=HorizontalAlignment.Right;
			TextCol.Format=format_sotien;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridColoredTextBoxColumn(de, 7);
			TextCol.MappingName = "stdatra";
			TextCol.HeaderText = "Đã thanh toán";
			TextCol.Width = 100;
			TextCol.Alignment=HorizontalAlignment.Right;
			TextCol.Format=format_sotien;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridColoredTextBoxColumn(de, 8);
			TextCol.MappingName = "conlai";
			TextCol.HeaderText = "Còn lại";
			TextCol.Width = 100;
			TextCol.Alignment=HorizontalAlignment.Right;
			TextCol.Format=format_sotien;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);
		}


		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void ena_object(bool ena)
		{
			find.Enabled=!ena;
			dataGrid1.Enabled=!ena;
			soqd.Enabled=ngayqd.Enabled=sohd.Enabled=ngayhd.Enabled=noidung.Enabled=sotien.Enabled=laisuat.Enabled=madt.Enabled=tendt.Enabled=ena;
			butLuu.Enabled=butBoqua.Enabled=ena;
			butMoi.Enabled=butSua.Enabled=butHuy.Enabled=butIn.Enabled=butKetthuc.Enabled=!ena;
		}

		private void emp_detail()
		{
			l_denghi=l_id=0;i_loai=1;dt.Clear();
			soqd.Text=sohd.Text=noidung.Text=sotien.Text=laisuat.Text=madt.Text=tendt.Text=diachi.Text="";
			ngayqd.Value=new DateTime(int.Parse(ngayts.Substring(6,4)),int.Parse(ngayts.Substring(3,2)),int.Parse(ngayts.Substring(0,2)),0,0,0,0);
			ngayhd.Value=new DateTime(int.Parse(ngayts.Substring(6,4)),int.Parse(ngayts.Substring(3,2)),int.Parse(ngayts.Substring(0,2)),0,0,0,0);
		}

		private void butMoi_Click(object sender, System.EventArgs e)
		{
			ena_object(true);
			emp_detail();
			bNew=true;			
			soqd.Focus();
		}

		private void butSua_Click(object sender, System.EventArgs e)
		{
			if (dtct.Rows.Count==0) return;
			ena_object(true);
			l_idold=l_id;
			bNew=false;
			soqd.Focus();
		}

		private bool Kiemtra()
		{
			if (madt.Text=="" || tendt.Text=="")
			{
				MessageBox.Show("Nhập đối tượng !",a.Msg);
				madt.Focus();
				return false;
			}
			r=a.getrowbyid(dtdt,"mavt='"+madt.Text+"'");
			if (r==null)
			{
				MessageBox.Show("Đối tượng không hợp lệ !",a.Msg);
				madt.Focus();
				return false;
			}
			lmadt=long.Parse(r["id"].ToString());
			decimal st=(sotien.Text!="")?decimal.Parse(sotien.Text):0;
			if (st==0)
			{
				MessageBox.Show("Nhập số tiền !",a.Msg);
				sotien.Focus();
				return false;
			}
			return true;
		}

		private void butLuu_Click(object sender, System.EventArgs e)
		{
			if (!Kiemtra()) return;
			l_id=(bNew)?a.getIdtheodoihd():l_id;
			itable = a.tableid("",table);
			if (bNew) a.upd_eve_tables(itable, i_userid, "ins");
			else
			{
				a.upd_eve_tables(itable, i_userid, "upd");
				a.upd_eve_upd_del(itable, i_userid, "upd",a.fields(table,"id="+l_id));
			}
			if (!a.upd_theodoihd(l_id,s_ngay,soqd.Text,ngayqd.Text,sohd.Text,ngayhd.Text,noidung.Text,lmadt,(sotien.Text!="")?decimal.Parse(sotien.Text):0,l_denghi,i_loai,(laisuat.Text!="")?decimal.Parse(laisuat.Text):0,s_mmyy,i_userid))
			{
				MessageBox.Show("Không cập nhật được thông tin hợp đồng !",a.Msg);
				return;
			}
			load_grid();
			ref_text(0);
			ena_object(false);
			butMoi.Focus();
		}

		private void butBoqua_Click(object sender, System.EventArgs e)
		{
			l_id=l_idold;
			ref_text(l_id);
			ena_object(false);			
			butMoi.Focus();
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			ref_text(0);
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
			try
			{
				if (dtct.Rows.Count==0) return;
				if (a.get_data("select * from "+table+" where stdatra>0 and id="+l_id).Tables[0].Rows.Count>0)
				{
					MessageBox.Show("Đã thanh toán không cho phép hủy !",a.Msg);
					return;
				}
				if (MessageBox.Show("Đồng ý hủy số phiếu này ?",a.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
				{
					a.execute_data("delete from "+table+" where id="+l_id);
					a.delrec(dtct,"id="+l_id);
					ref_text(0);
				}
			}
			catch{}
		}


		private void butIn_Click(object sender, System.EventArgs e)
		{
			if (dt.Rows.Count==0) return;
			sql="select a.id,a.soqd as sophieu,to_char(a.ngayqd,'dd/mm/yyyy') ngaysp,a.sohd,to_char(a.ngayhd,'dd/mm/yyyy') ngayhd,";
			sql+="c.tendt as madv,b.no,b.co,a.stien as sotien,a.stdatra as datra,b.ghichu,b.so,to_char(b.ngay,'dd/mm/yyyy') as ngay,b.sotien as sotiendot";
			sql+=" from "+user+".a_theodoihd a,"+user+".a_theodoihdct b,"+user+".a_dmdt c where a.id=b.id and a.madt=c.id and a.id="+l_id+"  order by b.stt";
			DataSet tmp=a.get_data(sql);
			frmReport f=new frmReport(a,tmp,"","KT_congno_dot.rpt");
			f.ShowDialog(this);		
		}

		private void find_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==find) RefreshChildren(find.Text);
		}

		private void RefreshChildren(string text)
		{
			try
			{
				CurrencyManager cm= (CurrencyManager)BindingContext[dataGrid1.DataSource];
				DataView dv=(DataView)cm.List;			
				sql="noidung like '%"+text.Trim()+"%' or tendt like '%"+text.Trim()+"%' or soqd like '%"+text.Trim()+"%' or sohd like '%"+text.Trim()+"%'";
				dv.RowFilter=sql;
			}
			catch(Exception ex){MessageBox.Show(ex.Message);}
		}

		private void find_Enter(object sender, System.EventArgs e)
		{
			find.Text="";
		}

		private void butFind_Click(object sender, System.EventArgs e)
		{
			/*frmTimthuoc f=new frmTimthuoc(d,s_mmyy,s_loai,i_nhom,i_userid,bAdmin);
			f.ShowDialog();*/
		}

		private void hoatdong_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");		
		}

		private void listdt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
			{
				try
				{
					DataRow r=a.getrowbyid(dtdt,"mavt='"+madt.Text+"'");
					if (r!=null)
					{
						diachi.Text=r["diachi"].ToString();
						noidung.Focus();
					}
				}
				catch{}		
			}
		}

		private void madt_Validated(object sender, System.EventArgs e)
		{
			DataRow r=a.getrowbyid(dtdt,"mavt='"+madt.Text+"'");
			if (r!=null)
			{
				tendt.Text=r["tendt"].ToString();
				diachi.Text=r["diachi"].ToString();
			}
			else madt.Text=tendt.Text=diachi.Text="";
		}

		
		private void Filter_dmdt(string ten)
		{
			try
			{
				CurrencyManager cm= (CurrencyManager)BindingContext[listdt.DataSource];
				DataView dv=(DataView)cm.List;
				dv.RowFilter="tendt like '%"+ten.Trim()+"%'";
			}
			catch{}
		}

		private void tendt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listdt.Focus();
			else if (e.KeyCode==Keys.Enter)
			{
				if (listdt.Visible)	listdt.Focus();
				else SendKeys.Send("{Tab}");
			}
		}

		private void tendt_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==tendt && !diachi.Enabled)
			{
				Filter_dmdt(tendt.Text);
				listdt.BrowseToDanhmuc(tendt,madt,noidung,50);
			}
		}

		private void soqd_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void laisuat_Validated(object sender, System.EventArgs e)
		{
			try
			{
				decimal st=(laisuat.Text!="")?decimal.Parse(laisuat.Text):0;
				laisuat.Text=st.ToString(format_sotien);
			}
			catch{}		
		}

		private void sotien_Validated(object sender, System.EventArgs e)
		{
			try
			{
				decimal st=(sotien.Text!="")?decimal.Parse(sotien.Text):0;
				sotien.Text=st.ToString(format_sotien);
			}
			catch{}
		}
	}
}
